package com.tsingteng.cosfun.ui.message.chat;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.ChatBean;
import com.tsingteng.cosfun.common.Constant;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.ui.mine.TerminalActivity;
import com.tsingteng.cosfun.utils.ImageUtils;
import com.tsingteng.cosfun.utils.SPUtils;
import com.tsingteng.cosfun.utils.TimeUtils;

import java.text.ParseException;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class ChatManyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "ChatManyAdapter";
    Context context;
    List<ChatBean.ListBean> mList;
    public static final int LEFT_TYPE = 1;
    public static final int RIGHT_TYPE = 2;

    public ChatManyAdapter(Context context, List<ChatBean.ListBean> chatList) {
        this.context = context;
        this.mList = chatList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        RecyclerView.ViewHolder viewHolder = null;
        View view = null;
        if(viewType == LEFT_TYPE){
            view = layoutInflater.inflate(R.layout.item_left_chat,parent,false);
            viewHolder = new LeftHolder(view);
        }else{
            view = layoutInflater.inflate(R.layout.item_right_chat,parent,false);
            viewHolder = new RightHolder(view);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//        int interval = 0;
//        try {
////            interval = Math.abs(TimeUtils.getInterval(mList.get(position).getCreateTime(), AppUtils.getChatTime()));
////            LogUtils.e(TAG,interval + "========>s");
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        if(holder instanceof LeftHolder){
            ImageUtils.LoadImage(context,mList.get(position).getSource().getImage(),((LeftHolder) holder).lIv);
            boolean today = false;
            try {
                today = TimeUtils.IsToday(TimeUtils.formatDateTime(mList.get(position).getCreateTime()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if(today){
                ((LeftHolder) holder).lTime.setText(TimeUtils.formatDateTimeHM(mList.get(position).getCreateTime()));
            }else{
                ((LeftHolder) holder).lTime.setText(TimeUtils.formatDateTime(mList.get(position).getCreateTime()));
            }
//            ((LeftHolder) holder).lTime.setVisibility(interval/60>5?View.VISIBLE:View.GONE);
            ((LeftHolder) holder).lContent.setText(mList.get(position).getContent());
            ((LeftHolder) holder).lIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int tag = 4;
                    Bundle bundle = new Bundle();
                    bundle.putInt("tag", tag);
                    bundle.putInt("user_id",mList.get(position).getSource().getId());
                    bundle.putString("worjId",mList.get(position).getId()+"");
                    bundle.putInt(Constant.TAG,1);
//                    bundle.putString(Constant.OTHER,"other");
                    TerminalActivity.showFragment(context, HomeMineFragment.class, bundle);
                }
            });
        }else{
            ImageUtils.LoadImage(context,mList.get(position).getSource().getImage(),((RightHolder) holder).rIv);
            boolean today = false;
            try {
                today = TimeUtils.IsToday(TimeUtils.formatDateTime(mList.get(position).getCreateTime()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if(today){
                ((RightHolder) holder).rTime.setText(TimeUtils.formatDateTimeHM(mList.get(position).getCreateTime()));
            }else{
                ((RightHolder) holder).rTime.setText(TimeUtils.formatDateTime(mList.get(position).getCreateTime()));
            }
//            ((RightHolder) holder).rTime.setVisibility(interval/60>5?View.VISIBLE:View.GONE);
            ((RightHolder) holder).rContent.setText(mList.get(position).getContent());
            ((RightHolder) holder).rIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("user_id",mList.get(position).getSource().getId());
                    bundle.putInt(Constant.TAG,1);
                    TerminalActivity.showFragment(context, HomeMineFragment.class, bundle);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList==null?0:mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        int id = mList.get(position).getSource().getId();
        if(id != SPUtils.init().getInt("profile_id")){
            return LEFT_TYPE;
        }else{
            return RIGHT_TYPE;
        }
    }

    private class LeftHolder extends RecyclerView.ViewHolder{
        CircleImageView lIv;
        TextView lTime,lContent;
        public LeftHolder(View itemView) {
            super(itemView);
            lTime = itemView.findViewById(R.id.tv_left_chat_time);
            lContent = itemView.findViewById(R.id.tv_left_chat_content);
            lIv = itemView.findViewById(R.id.iv_circle_left_chat);
        }
    }

    private class RightHolder extends RecyclerView.ViewHolder{
        CircleImageView rIv;
        TextView rTime,rContent;
        public RightHolder(View itemView) {
            super(itemView);
            rTime = itemView.findViewById(R.id.tv_right_chat_time);
            rContent = itemView.findViewById(R.id.tv_right_chat_content);
            rIv = itemView.findViewById(R.id.iv_circle_right_chat);
        }
    }
}
